<?php !defined('IN_ONEZ') && exit('Access Denied');?>
<?php
include(ONEZ_ROOT.'/source/header.php');
?>
<link href="images/login.css" rel="stylesheet" type="text/css" />
<?
if($_POST){
  $ucurl=GP('ucurl');
  $ucip=GP('ucip');
  $ucpw=GP('ucpw');
  $app_name=GP('app_name');
  !defined('UC_API') && define('UC_API',true);
  include_once ONEZ_ROOT.'/uc_client/client.php';
	global $ucdbcharset;
  $ucinfo=post($ucurl.'/index.php?m=app&a=ucinfo&release='.UC_CLIENT_RELEASE);
  list($status, $ucversion, $ucrelease, $uccharset, $ucdbcharset, $apptypes) = explode('|', $ucinfo);
  $status != 'UC_STATUS_OK' && exit('UCenter 的 URL 地址可能填写错误，请检查');
  $dbcharset = strtolower($dbcharset ? str_replace('-', '', $dbcharset) : $dbcharset);
	$ucdbcharset = strtolower($ucdbcharset ? str_replace('-', '', $ucdbcharset) : $ucdbcharset);
	UC_CLIENT_VERSION > $ucversion && exit('您的 UCenter 服务端版本过低，请升级 UCenter 服务端到最新版本，并且升级，下载地址：http://www.comsenz.com/ 。');
	$dbcharset && $ucdbcharset != $dbcharset && exit('UCenter 数据库字符集与当前应用字符集不一致');
  $app_type='OTHER';
  $app_url=$homepage;
  $app_name=ucdata($app_name,1);
  $postdata = "m=app&a=add&ucfounder=&ucfounderpw=".urlencode($ucpw)."&apptype=".urlencode($app_type)."&appname=".urlencode($app_name)."&appurl=".urlencode($app_url)."&appip=&appcharset=".$ucdbcharset.'&appdbcharset='.$ucdbcharset.'&'.$app_tagtemplates.'&release='.UC_CLIENT_RELEASE;
  $ucconfig = post($ucurl.'/index.php', $postdata);
  empty($ucconfig) && exit('向 UCenter 添加应用错误');
  $ucconfig=='-1' && exit('UCenter 创始人密码错误，请重新填写');
  list($appauthkey, $appid) = explode('|', $ucconfig);
  if(empty($appauthkey) || empty($appid)) {
  	exit('通信失败，请检查 UCenter 的URL 地址是否正确');
  }
	list($appauthkey, $appid, $ucdbhost, $ucdbname, $ucdbuser, $ucdbpw, $ucdbcharset, $uctablepre, $uccharset, $ucapi, $ucip) = explode('|', $ucconfig);
  
	$link = mysql_connect($ucdbhost, $ucdbuser, $ucdbpw, 1);
	$uc_connnect = $link && mysql_select_db($ucdbname, $link) ? 'mysql' : '';
  $ucenter=<<<ONEZ
define('UC_CONNECT', '$uc_connnect');
define('UC_DBHOST', '$ucdbhost');
define('UC_DBUSER', '$ucdbuser');
define('UC_DBPW', '$ucdbpw');
define('UC_DBNAME', '$ucdbname');
define('UC_DBCHARSET', '$ucdbcharset');
define('UC_DBTABLEPRE', '`$ucdbname`.$uctablepre');
define('UC_DBCONNECT', '0');
define('UC_KEY', '$appauthkey');
define('UC_API', '$ucurl');
define('UC_CHARSET', '$uccharset');
define('UC_IP', '$ucip');
define('UC_APPID', '$appid');
define('UC_PPP', '20');
ONEZ;
	echo '恭喜！UC整合成功。请将以下代码拷贝到config.php中<br /><br /><textarea style="width:500px;height:260px">'.$ucenter.'</textarea>';
	exit();
}
?>
<h1 style="margin:20px">自动安装到UCenter中</h1>
<form id="divForm" method="post">
<table class="tb">
  <tr>
    <td width="150"><strong>当前应用名称</strong></td>
    <td><input name="app_name" type="text" class="onez-input-text" value="二维码登录" /></td>
    <td></td>
  </tr>
  <tr>
    <td><strong>UCenter 的 URL</strong></td>
    <td><input name="ucurl" type="text" class="onez-input-text" value="" /></td>
    <td></td>
  </tr>
  <tr>
    <td><strong>UCenter 创始人密码</strong></td>
    <td><input name="ucpw" type="password" class="onez-input-text" value="" /></td>
    <td></td>
  </tr>
</table>
<button id="registersubmit" name="btn" value="submit" type="submit">确定安装</button>
</form>

<?php include(ONEZ_ROOT.'/source/footer.php');?>